<?php

/**
 * User Homepage
 * @package uniCRM
 * @version 1.0b
 */

session_start();

// Configuration file
include 'configuration.php';
// Functions
include 'inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	header('Location: index.php');
	exit();
endif;

// Include all objects
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// ---------- START: HEAD
include $configuration['webroot'].'inc/head.php';
// ---------- END: HEAD

// ---------- START: MENU
include $configuration['webroot'].'inc/menu.php';
// ---------- END: MENU

// ---------- START: BODY ?>

<div id="title" style="background-image:url('css/img/dashboard.png')">Dashboard</div>

<div id="stat-renderer-1" class="statsfield-mini" style="float:left"></div>
<div id="stat-renderer-2" class="statsfield-mini" style="margin-left:52%"></div>

<?php
$activity = new Activity();
$activityList = $activity->GetList(Array(Array('type', '<>', 'TASK'), Array('crmuserId', '=', unserialize($_SESSION['current_user'])->crmuserId)), 'activityId', false, '0,20');
?>
<div style="float:left;width:50%">
	<h3 class="subtitle mini">Mes 20 dernières activités</h3>
	<table cellpadding="0" cellspacing="0" border="0" class="table-objects mini-table corner-all">
		<tr>
			<th>#</th>
			<th>Type</th>
			<th>Priorité</th>
			<th>Contact</th>
			<th>Date</th>
			<th>Sujet</th>
		</tr>
		<?php
		if (count($activityList) > 0):
			foreach ($activityList as $activity):
				$contact = $activity->GetContact();
				$crmuser = $activity->GetCrmuser();
				$crmcontact = $crmuser->GetContact();
				$date = explode('-', substr($activity->date, 0, 10));
				$date = $date[2].'/'.$date[1].'/'.$date[0].' à '.substr($activity->date, 11, 5);
				
				$res  = '<tr>';
				$res .= '<td><a href="activities.php?operation=edit&id='.$activity->activityId.'">'.$activity->activityId.'</a></td>';
				$res .= '<td>'.$activityType[$activity->type].'</td>';
				$res .= '<td>'.$priority[$activity->priority].'</td>';
				$res .= '<td><a href="contacts.php?id='.$contact->contactId.'&operation=edit">'.$contact->lastname.' '.$contact->firstname.'</a></td>';
				$res .= '<td>'.$date.'</td>';
				$res .= '<td>'.((strlen($activity->title) > 50) ? substr($activity->title).'...' : $activity->title).'</td>';
				$res .= '</tr>';
				echo $res;
			endforeach;
		else:
			echo '<tr><td colspan="8" class="ui-state-error">Aucune donnée</td></tr>';
		endif;
		?>
	</table>
</div>

<div style="margin-left:50%">
	<?php
	$activity = new Activity();
	$activityList = $activity->GetList(Array(Array('crmuserId', '=', unserialize($_SESSION['current_user'])->crmuserId), Array('type', '=', 'TASK')), 'date', false, '0,20');
	?>
	<h3 class="subtitle mini">Mes 20 dernières tâches</h3>
	<table cellpadding="0" cellspacing="0" border="0" class="table-objects mini-table corner-all" style="float:right">
		<tr>
			<th>#</th>
			<th>Type</th>
			<th>Priorité</th>
			<th>Contact</th>
			<th>Date</th>
			<th>Sujet</th>
		</tr>
		<?php
		if (count($activityList) > 0):
			foreach ($activityList as $activity):
				$contact = $activity->GetContact();
				$crmuser = $activity->GetCrmuser();
				$crmcontact = $crmuser->GetContact();
				$date = explode('-', substr($activity->date, 0, 10));
				$date = $date[2].'/'.$date[1].'/'.$date[0].' à '.substr($activity->date, 11, 5);
				
				$res  = '<tr>';
				$res .= '<td><a href="activities.php?operation=edit&id='.$activity->activityId.'">'.$activity->activityId.'</a></td>';
				$res .= '<td>'.$activityType[$activity->type].'</td>';
				$res .= '<td>'.$priority[$activity->priority].'</td>';
				$res .= '<td><a href="contacts.php?id='.$contact->contactId.'&operation=edit">'.$contact->lastname.' '.$contact->firstname.'</a></td>';
				$res .= '<td>'.$date.'</td>';
				$res .= '<td>'.((strlen($activity->title) > 50) ? substr($activity->title).'...' : $activity->title).'</td>';
				$res .= '</tr>';
				echo $res;
			endforeach;
		else:
			echo '<tr><td colspan="8" class="ui-state-error">Aucune donnée</td></tr>';
		endif;
		?>
	</table>
</div>

<div class="break"></div>

<?php

// ---------- END: BODY

// ---------- START: FOOT
include $configuration['webroot'].'inc/foot.php';
// ---------- END: FOOT

// ---------- JAVASCRIPTS AND END
include $configuration['webroot'].'inc/js.php'; ?>
<script type="text/javascript" src="js/dashboard.js"></script>

</body>
</html>